package com.github.glideplus.impl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;



/**
 * Created by zoudong on 2017/2/19.
 */

public class GlideLoader {

    private   RequestManager requestManager;
    private  DrawableRequestBuilder<String> requestBuilder;
    private   Context context;
    public static GlideLoader with(Context context){
        return new GlideLoader(context);
    }

    private GlideLoader(Context context) {
        this.context = context;
        requestManager = Glide.with(context);
    }

    public GlideLoader load(String url) {
        if(requestManager ==null){
            throw new RuntimeException("Please call with()");
        }
        requestBuilder =requestManager.load(url);
        return  this;
    }
    public GlideLoader build(DisplayOptions builder){
        if(requestBuilder ==null){
            throw new RuntimeException("Please call load()");
        }
        List<Transformation<Bitmap>> transformations = new ArrayList<>();
        if (builder.isCenterCrop) {
            transformations.add(new CenterCrop(context));
        }
        if (builder.isCircle) {
            transformations.add(new CropCircleTransformation(context));
        }
        if (builder.roundModel != null) {
            if (builder.roundModel.getRounded_Radius() > 0 || builder.roundModel.getRounded_Margin() > 0) {
                transformations.add(new RoundedCornersTransformation(context, builder.roundModel.getRounded_Radius(), builder.roundModel.getRounded_Margin(), RoundedCornersTransformation.CornerType.valueOf(builder.roundModel.getCornerType().name())));
            }
        }
        Transformation<Bitmap>[] array = new Transformation[transformations.size()];
        for (int i = 0; i < transformations.size(); i++) {
            array[i] = transformations.get(i);
        }
        requestBuilder.bitmapTransform(array).diskCacheStrategy(DiskCacheStrategy.NONE);
        return  this;
    }


    public void into(ImageView view) {
        if (requestBuilder == null) {
                throw new RuntimeException("Please call load()");
        }
        requestBuilder.into(view);
    }

    public  static  BitmapDrawable transformDrawable(Context context, Drawable drawable, Transformation<Bitmap> transform,  int outWidth, int outHeight) {
        // render original
        BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
        Bitmap bitmap = bitmapPool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, outWidth, outHeight);
        drawable.draw(canvas);
        // make rounded
        Resource<Bitmap> original = BitmapResource.obtain(bitmap, bitmapPool);
        Resource<Bitmap> rounded = transform.transform(original, outWidth, outHeight);
        if (!original.equals(rounded)) {
            original.recycle();
        }
        return new BitmapDrawable(context.getResources(), rounded.get());
    }
    private Bitmap.Config getSaleConfig(Bitmap toTransform) {
        return toTransform.getConfig() != null
                ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
    }
}
